package com.person.ntv.shiro.filter;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.shiro.web.filter.mgt.FilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;

public class ExtPathMatchingFilterChainResolve extends PathMatchingFilterChainResolver{
	private ExtDefaultFilterChainManager extDefaultFilterChainManager;

    public void setExtDefaultFilterChainManager(ExtDefaultFilterChainManager extDefaultFilterChainManager) {
        this.extDefaultFilterChainManager = extDefaultFilterChainManager;
        setFilterChainManager(extDefaultFilterChainManager);
    }

    public FilterChain getChain(ServletRequest request, ServletResponse response, FilterChain originalChain) {
        FilterChainManager filterChainManager = getFilterChainManager();
        if (!filterChainManager.hasChains()) {
            return null;
        }

        String requestURI = getPathWithinApplication(request);

        List<String> chainNames = new ArrayList<String>();
        //the 'chain names' in this implementation are actually path patterns defined by the user.  We just use them
        //as the chain name for the FilterChainManager's requirements
        for (String pathPattern : filterChainManager.getChainNames()) {
            // If the path does match, then pass on to the subclass implementation for specific checks:
            if (pathMatches(pathPattern, requestURI)&&!"/**".equals(pathPattern)) {
                chainNames.add(pathPattern);
            }
        }
        if(chainNames.size() == 0) {
        	chainNames.add("/**");
        }
        return extDefaultFilterChainManager.proxy(originalChain, chainNames);
    }
}
